import 'package:flutter/material.dart';
import 'package:flutter_base/provider_demo4/models/task.dart';
import 'package:provider/provider.dart';

class SecondPage extends StatefulWidget {
  const SecondPage(this.taskId, {Key? key}) : super(key: key);

  final int taskId;

  @override
  State<SecondPage> createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  int completion = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('二级页面')),
      body: Center(
        child: Column(
          children: <Widget>[
            Container(
              decoration: const BoxDecoration(color: Colors.lightBlue),
              padding: const EdgeInsets.all(15.0),
              child: Selector<TaskList, Task>(
                selector: (_, taskList) => taskList.tasks.singleWhere((e) => e.id == widget.taskId),
                builder: (context, task, child) => Row(
                  children: <Widget>[
                    Text('total: ${task.stats.total}'),
                    const Text('/'),
                    Text('completion: ${task.stats.completion}'),
                  ],
                ),
                shouldRebuild: (previous, next) {
                  debugPrint('previous value: ${previous.toString()}');
                  debugPrint('next value: ${next.toString()}');
                  return previous != next;
                },
              ),
            ),
            const SizedBox(height: 30.0),
            ElevatedButton(
              onPressed: () {
                Provider.of<TaskList>(context, listen: false).incrementCompletion(widget.taskId);
              },
              child: const Text('更新完成数'),
            )
          ],
        ),
      ),
    );
  }
}
